﻿//--------------------------------------------------------------------
// All Rights Reserved , Copyright (C) 2012 ,  TECH, Ltd. 
//--------------------------------------------------------------------

using System;
using System.Collections.Generic;
using System.Data;
using System.Reflection;
using System.ServiceModel;

namespace RDIFramework.ServiceAdapter
{
    using RDIFramework.BusinessEntity;
    using RDIFramework.IService;
    using RDIFramework.Utilities;
	

	/// <summary>
	/// CiDbLinkDefineService
	/// 服务层
	/// 
	/// 修改记录
	/// 
	///		2012-03-02 版本：1.0 EricHu 建立。
	///		
	/// 版本：1.0
	///
	/// <author>
	///		<name>EricHu</name>
	///		<date>2012-03-02</date>
	/// </author> 
	/// </summary>
	[ServiceBehavior(IncludeExceptionDetailInFaults = true)]
	public class DbLinkDefineService : System.MarshalByRefObject, IDbLinkDefineService
	{
		private string serviceName = RDIFrameworkMessage.DbLinkDefineService;
		
		/// <summary>
		/// .NET快速开发整合框架（RDIFramework.NET）数据库连接
		/// </summary>
		private readonly string RDIFrameworkDbConection = SystemInfo.RDIFrameworkDbConection;


		/// <summary>
		/// 新增数据库连接
		/// </summary>
		/// <param name="userInfo">用户</param>
		/// <param name="entity">实体</param>
		/// <param name="statusCode">返回状态码</param>
		/// <param name="statusMessage">返回状态信息</param>
		/// <returns>主键</returns>
		public string Add(UserInfo userInfo, CiDbLinkDefineEntity entity, out string statusCode, out string statusMessage)
		{		
			string returnValue = string.Empty;            
			statusCode = string.Empty;
            
			using (IDbProvider dbProvider = DbFactoryProvider.GetProvider(SystemInfo.RDIFrameworkDbType))
			{
				try
				{
					dbProvider.Open(RDIFrameworkDbConection);
					CiDbLinkDefineManager manager = new CiDbLinkDefineManager(dbProvider, userInfo);					
					returnValue = manager.Add(entity, out statusCode);
					statusMessage = manager.GetStateMessage(statusCode);
				}
				catch (Exception ex)
				{
					CiExceptionManager.LogException(dbProvider, userInfo, ex);
					throw ex;
				}
				finally
				{
					dbProvider.Close();
				}
			}
			return returnValue;
		}

		/// <summary>
		/// 取得列表
		/// </summary>
		/// <param name="userInfo">用户</param>
		/// <returns>数据表</returns>
        public DataTable GetDT(UserInfo userInfo)
        {

            DataTable dataTable = new DataTable(CiDbLinkDefineTable.TableName);

            using (IDbProvider dbProvider = DbFactoryProvider.GetProvider(SystemInfo.RDIFrameworkDbType))
            {
                try
                {
                    dbProvider.Open(RDIFrameworkDbConection);
                    // 取得列表
                    CiDbLinkDefineManager manager = new CiDbLinkDefineManager(dbProvider, userInfo);
                    dataTable = manager.GetDT(CiDbLinkDefineTable.FieldDeleteMark, 0, CiDbLinkDefineTable.FieldSortCode);
                    dataTable.TableName = CiDbLinkDefineTable.TableName;
                }
                catch (Exception ex)
                {
                    CiExceptionManager.LogException(dbProvider, userInfo, ex);
                    throw ex;
                }
                finally
                {
                    dbProvider.Close();
                }
            }
            return dataTable;
        }

		/// <summary>
		/// 取得实体
		/// </summary>
		/// <param name="userInfo">用户</param>
		/// <param name="id">主键</param>
		/// <returns>实体</returns>
		public CiDbLinkDefineEntity GetEntity(UserInfo userInfo, string id)
		{
			CiDbLinkDefineEntity entity = null;

            using (IDbProvider dbProvider = DbFactoryProvider.GetProvider(SystemInfo.RDIFrameworkDbType))
            {
                try
                {
                    dbProvider.Open(RDIFrameworkDbConection);
                    CiDbLinkDefineManager manager = new CiDbLinkDefineManager(dbProvider, userInfo);
                    entity = manager.GetEntity(id);
                }
                catch (Exception ex)
                {
                    CiExceptionManager.LogException(dbProvider, userInfo, ex);
                    throw ex;
                }
                finally
                {
                    dbProvider.Close();
                }
            }

			return entity;
		}

		/// <summary>
		/// 更新实体
		/// </summary>
		/// <param name="userInfo">用户</param>
		/// <param name="entity">实体</param>
		/// <param name="statusCode">返回状态码</param>
		/// <param name="statusMessage">返回状态信息</param>
		/// <returns>影响行数</returns>
		public int Update(UserInfo userInfo, CiDbLinkDefineEntity entity, out string statusCode, out string statusMessage)
		{		

            int returnValue = 0;
            statusCode = string.Empty;

            using (IDbProvider dbProvider = DbFactoryProvider.GetProvider(SystemInfo.RDIFrameworkDbType))
            {
	            try
	            {
		            dbProvider.Open(RDIFrameworkDbConection);
		            CiDbLinkDefineManager manager = new CiDbLinkDefineManager(dbProvider, userInfo);		            
		            returnValue = manager.Update(entity, out statusCode);
		            statusMessage = manager.GetStateMessage(statusCode);
	            }
	            catch (Exception ex)
	            {
		            CiExceptionManager.LogException(dbProvider, userInfo, ex);
		            throw ex;
	            }
	            finally
	            {
		            dbProvider.Close();
	            }
            }

            return returnValue;
		}

		/// <summary>
		/// 根据主键值取得数据库连接定义
		/// </summary>
		/// <param name="userInfo">用户</param>
		/// <param name="ids">主键</param>
		/// <returns>数据表</returns>
		public DataTable GetDTByIds(UserInfo userInfo, string[] ids)
		{
			DataTable dataTable = new DataTable(CiDbLinkDefineTable.TableName);

			using (IDbProvider dbProvider = DbFactoryProvider.GetProvider(SystemInfo.RDIFrameworkDbType))
			{
				try
				{
					dbProvider.Open(RDIFrameworkDbConection);
					CiDbLinkDefineManager manager = new CiDbLinkDefineManager(dbProvider, userInfo);
					dataTable = manager.GetDT(CiDbLinkDefineTable.FieldId, ids, CiDbLinkDefineTable.FieldSortCode);
					dataTable.TableName = CiDbLinkDefineTable.TableName;
				}
				catch (Exception ex)
				{
					CiExceptionManager.LogException(dbProvider, userInfo, ex);
					throw ex;
				}
				finally
				{
					dbProvider.Close();
				}
			}

			return dataTable;
		}
		
		/// <summary>
		/// 根据条件取得数据库连接数据表
		/// </summary>
		/// <param name="userInfo">用户</param>
		/// <param name="names">字段</param>
		/// <param name="values">值</param>
		/// <returns>数据表</returns>
		public DataTable GetDTByValues(UserInfo userInfo, string[] names, object[] values)
		{
			DataTable dataTable = new DataTable(CiDbLinkDefineTable.TableName);

			using (IDbProvider dbProvider = DbFactoryProvider.GetProvider(SystemInfo.RDIFrameworkDbType))
			{
				try
				{
					dbProvider.Open(RDIFrameworkDbConection);
					// 依條件獲取資料列表
					CiDbLinkDefineManager manager = new CiDbLinkDefineManager(dbProvider, userInfo);
					dataTable = manager.GetDT(names, values);
					dataTable.TableName = CiDbLinkDefineTable.TableName;
				}
				catch (Exception ex)
				{
					CiExceptionManager.LogException(dbProvider, userInfo, ex);
					throw ex;
				}
				finally
				{
					dbProvider.Close();
				}
			}

			return dataTable;
		}			
		
		/// <summary>
		/// 刪除資料
		/// </summary>
		/// <param name="userInfo">用户</param>
		/// <param name="id">主键</param>
		/// <returns>数据表</returns>
		public int Delete(UserInfo userInfo, string id)
		{			
			int returnValue = 0;			
			using (IDbProvider dbProvider = DbFactoryProvider.GetProvider(SystemInfo.RDIFrameworkDbType))
			{
				try
				{
					dbProvider.Open(RDIFrameworkDbConection);
					CiDbLinkDefineManager manager = new CiDbLinkDefineManager(dbProvider, userInfo);
					returnValue = manager.Delete(id);
				}
				catch (Exception ex)
				{
					CiExceptionManager.LogException(dbProvider, userInfo, ex);
					throw ex;
				}
				finally
				{
					dbProvider.Close();
				}
			}				

			return returnValue;
		}
		
		/// <summary>
		/// 批量刪除数据
		/// </summary>
		/// <param name="userInfo">用户</param>
		/// <param name="ids">主键陣列</param>
		/// <returns>影响行数</returns>
		public int BatchDelete(UserInfo userInfo, string[] ids)
		{		

			int returnValue = 0;

			using (IDbProvider dbProvider = DbFactoryProvider.GetProvider(SystemInfo.RDIFrameworkDbType))
			{
				try
				{
					dbProvider.Open(RDIFrameworkDbConection);
					// 开始事务
					dbProvider.BeginTransaction();
					CiDbLinkDefineManager manager = new CiDbLinkDefineManager(dbProvider, userInfo);
					returnValue = manager.Delete(ids);
					// 提交事务
					dbProvider.CommitTransaction();
				}
				catch (Exception ex)
				{
					// 撤銷資料庫事務
					dbProvider.RollbackTransaction();
					CiExceptionManager.LogException(dbProvider, userInfo, ex);
					throw ex;
				}
				finally
				{
					dbProvider.Close();
				}
			}

			return returnValue;
		}

		/// <summary>
		/// 批量设置删除标志
		/// </summary>
		/// <param name="userInfo">用户</param>
		/// <param name="ids">主键陣列</param>
		/// <returns>影响行数</returns>
		public int SetDeleted(UserInfo userInfo, string[] ids)
		{
			int returnValue = 0;

			using (IDbProvider dbProvider = DbFactoryProvider.GetProvider(SystemInfo.RDIFrameworkDbType))
			{
				try
				{
					dbProvider.Open(RDIFrameworkDbConection);
					CiDbLinkDefineManager manager = new CiDbLinkDefineManager(dbProvider, userInfo);
					returnValue = manager.SetDeleted(ids);
				}
				catch (Exception ex)
				{
					CiExceptionManager.LogException(dbProvider, userInfo, ex);
					throw ex;
				}
				finally
				{
					dbProvider.Close();
				}
			}

			return returnValue;
		}
	}
}
